﻿Public Class Articulos
    Private binding1 As New BindingSource
    Private _refArt As String = vbNullString
    Private _fromPedido As Boolean = False
    Private _catalog As String = vbNullString
    Private _Fabric As Byte = 0

    Public WriteOnly Property fromPedido() As Boolean
        Set(ByVal value As Boolean)
            _fromPedido = value
        End Set
    End Property

    Public WriteOnly Property Catalog() As String
        Set(ByVal value As String)
            _catalog = value
        End Set
    End Property

    Public WriteOnly Property Fabric() As Byte
        Set(ByVal value As Byte)
            _Fabric = value
        End Set
    End Property

    Public ReadOnly Property refArt() As String
        Get
            Return _refArt
        End Get
    End Property

    Private Sub Articulos_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles _
    Me.GotFocus, CmbFabricante.SelectedValueChanged
        If IsNumeric(CmbFabricante.SelectedValue) Then
            _Fabric = CByte(CmbFabricante.SelectedValue)
            updateCatalogo()
        End If
    End Sub

    Private Sub Articulos_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Dim dibujo As Image
        setFabricante()
        DataGridView1.DataSource = binding1
    End Sub

    Public Sub ShowMyImage(ByVal fileToDisplay As String)
        Try
            Dim newImage As Image = Image.FromFile(fileToDisplay)
            ' Stretches the image to fit the pictureBox. 
            ArtImagen.SizeMode = PictureBoxSizeMode.StretchImage
            'pictureBox1.ClientSize = New Size(xSize, ySize)
            ArtImagen.Image = newImage
        Catch
            ArtImagen.Image = Nothing
        End Try
    End Sub

    Private Sub setFabricante()
        Dim sqlStr As String
        If (_fromPedido) Then
            sqlStr = "SELECT idFabricante, Nombre FROM Fabricantes WHERE idFabricante = " & _Fabric.ToString
        Else
            sqlStr = "SELECT idFabricante, Nombre FROM Fabricantes ORDER BY idFabricante"
        End If
        With CmbFabricante
            .DataSource = DataAccess.GetRecords(sqlStr, CommandType.Text)
            .DisplayMember = "Nombre"
            .ValueMember = "idFabricante"
        End With
    End Sub

    Private Sub updateCatalogo()
        Dim sqlStr As String
        If (_fromPedido) Then
            sqlStr = "SELECT DISTINCT Catalogo FROM Articulos WHERE Fabricante = " & _
                        _Fabric.ToString & " AND Catalogo ='" & _catalog & "'"
        Else
            sqlStr = "SELECT DISTINCT Catalogo FROM Articulos WHERE Fabricante = " & _
                        _Fabric.ToString
        End If
        With CmbCatalogo
            .DataSource = DataAccess.GetRecords(sqlStr, CommandType.Text)
            .DisplayMember = "Catalogo"
            .ValueMember = "Catalogo"
        End With
        refrescarDataGrid()
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
    Handles Button1.Click
        Me.Close()
        Me.Dispose()
    End Sub

    Public Sub refrescarDataGrid()
        Dim sqlStr As String = "SELECT [IdArticulo],[Referencia],[Descripcion],[Dimensiones],CAST(Puntos  AS DECIMAL(10,2)) AS Puntos FROM [furniture].[dbo].[Articulos] " & _
                                " WHERE Fabricante = " & _Fabric.ToString & _
                                " AND Catalogo = '" & _catalog & "' ORDER BY [Referencia]"
        Try
            binding1.DataSource = DataAccess.GetRecords(sqlStr, CommandType.Text)
        Catch ex As Exception
            Console.WriteLine(ex.Message)
        End Try
        'nuestroThread.Abort()
        Main.ToolStripStatusLabel1.Text = ""
    End Sub

    Private Sub DataGridView1_CellDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellDoubleClick
        If e.RowIndex >= 0 Then
            _refArt = sender.Rows(e.RowIndex).Cells(1).Value.ToString
            If _fromPedido Then
                Me.Close()
            End If
        End If
    End Sub

    Private Sub DataGridView1_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) _
    Handles DataGridView1.CellEnter
        Dim filename As String = My.Application.Info.DirectoryPath + "\Imagen\" & sender.Rows(e.RowIndex).Cells(1).Value.ToString & ".gif"
        ShowMyImage(filename)
    End Sub

    Private Sub CmbCatalogo_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) _
        Handles CmbCatalogo.SelectedIndexChanged
        If Not _fromPedido Then _catalog = CmbCatalogo.SelectedValue.ToString
        refrescarDataGrid()
    End Sub

    Private Sub DataGridView1_DataBindingComplete(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewBindingCompleteEventArgs) Handles DataGridView1.DataBindingComplete
        With DataGridView1
            .Columns("IdArticulo").Visible = False
            .AutoResizeColumns()
        End With
    End Sub
End Class